package com.blensmile.utils;

import android.databinding.repacked.google.common.annotations.VisibleForTesting;
import android.databinding.repacked.google.common.util.concurrent.ExecutionError;
import android.os.AsyncTask;
import android.support.annotation.ColorInt;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.blensmile.encode.EncodeUtils;
import com.blensmile.network.models.VoiceGradeBean;
import com.google.gson.Gson;


import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Blensmile on 2017/3/17.
 */

public class VoiceGrader extends AsyncTask<String,Object,VoiceGradeBean> {

    public static final String HeadUrl ="http://10.0.1.217:3001/minos";
    private GraderCallback callback;
    private String formid,input,output;

    //execute的时候要传参数啊
    public VoiceGrader(/*String formid,String input,String output,*/GraderCallback callback){
        this.callback = callback;
//        this.formid = formid;
//        this.input = input;
//        this.output = output;
    }

    private VoiceGradeBean getData(String formid,File file){
        OkHttpClient client = new OkHttpClient();
        RequestBody formbody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("os","android")
                .addFormDataPart("formid",formid)
                .addFormDataPart("file","file",RequestBody.create(MediaType.parse("application/octet-stream"),file))
                .build();
        Request request = new Request.Builder()
                .url(HeadUrl)
                .post(formbody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String res = response.body().string();
            VoiceGradeBean bean = new Gson().fromJson(res,VoiceGradeBean.class);
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected VoiceGradeBean doInBackground(String... params) {
        try {
            if(params.length == 3){
                formid = params[0];
                input = params[1];
                output = params[2];
            }
            if(TextUtils.isEmpty(formid)||TextUtils.isEmpty(input)||TextUtils.isEmpty(output)){
                return null;
            }
            if (EncodeUtils.encodeFile(input, output)) {
                File file = new File(output);
                if (file.length() > 100 && file.isFile()) {
                    callback.onEncoded();
                    return getData(formid,file);
                }
            }
        }catch (Exception e){
        }
        return null;
    }

    @Override
    protected void onPostExecute(VoiceGradeBean bean) {
        if(bean != null){
            callback.onGraded(bean);
        }else {
            callback.onErro();
        }
    }

    public interface GraderCallback{
        //主线程
        void onErro();
        //子线程
        void onEncoded();
        //主线程
        void onGraded(VoiceGradeBean data);
    }

    public static SpannableString getCorrected(String text,String flag){
        if(TextUtils.isEmpty(text)|| TextUtils.isEmpty(flag)){
            return null;
        }
        String[] source = text.split("[ ;；]+");
        String[] flagArray = flag.split("[ ;；]+");
        @ColorInt int color = 0xffff0000;
        if(source.length == flagArray.length){
            SpannableString span = new SpannableString(text);
            int index = 0;
            for(int i = 0;i<source.length;i++){
                if("BAD".equals(flagArray[i])){
                    index = text.indexOf(source[i],index);
                    span.setSpan(new ForegroundColorSpan(color),index,index+source[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += 1;
                }
            }
            return span;
        }
        return null;
    }
}
